# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = "restructuredtext en"

from .. import formlib as layout

LEFT, RIGHT = layout.LEFT, layout.RIGHT


class StreetsAlleysLayout(layout.Layout):
    """
    static block (top):
        - reserves. (above >2 else below. _offset if holdcards. ncards if
                     reserve_texts).
        - rows. (either side foundations. _offset if playcards).
          foundations. (vert SUITS. max of 5 rows)

    dynamic block (bottom):  align LEFT or RIGHT side. default RIGHT.
        - talon (corner). waste (if Game._waste_. wastecards ignored).

    :cvar func _offset: dict('ycards': value)
    """
    _offset = lambda self, cards: {'xcards': cards}

    def create(self, rows, ncols=2, playcards=16, bases=None,
               reserves=0, reserve_texts=False, holdcards=0,
               align=LEFT, texts=False, **kw):
        l, s = self, self.s
        bases = bases if type(bases) in (list, tuple) else l.SUITS
        f_rows = min(5, l.NSUIT if bases is l.SUITS else len(bases))
        row = len(l._get_row_size(rows, ncols))
        top = reserves <= 2
        x, y = l.XM, l.YM + int(reserves !=0) * l.YS
        y += l.TEXT_HEIGHT if top and reserve_texts else 0
        l._set_rows(x=l.XM, y=y, rows=row, ncols=ncols / 2,
                    **l._offset(playcards))
        l._set_vert_foundations(l.size.width + l.XM, y, f_rows, bases)
        l._set_rows(x=l.size.width + l.XM, y=y, rows=row, ncols=ncols / 2,
                    **l._offset(playcards))
        x = (l.size.width - reserves * l.XS) / 2
        y = l.YM if top else l.size.height
        y += 0 if top and reserve_texts else l.TEXT_HEIGHT
        anchor = 's' if top else 'n'
        l._set_horz_reserves(x=x, y=y, reserves=reserves,
                             reserve_texts=reserve_texts, anchor=anchor,
                             **l._offset(holdcards))
        dx = l.XM if align == LEFT else l.size.width - l.XS
        dy = l.YM if reserves > 0 else l.size.height
        l._set_horz_talon(x=dx, y=dy, texts=texts, align=LEFT,
                          anchor='s', wastecards=0)

